package com.example.zuoye;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

public class Main2Activity extends AppCompatActivity {
    private int[] imageRes={R.mipmap.a1,R.mipmap.a2,R.mipmap.a3,
                            R.mipmap.a4,R.mipmap.a5,R.mipmap.a6,
                            R.mipmap.a7,R.mipmap.a8,R.mipmap.a9,};
    private String[] name={"QQ","WIFI","PS","flash","钱包","爱奇艺","抖音","百度云","计算机"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        GridView gridView=findViewById(R.id.gridview);
        int length=imageRes.length;

        ArrayList<HashMap<String,Object>>listImageRes=new ArrayList<HashMap<String,Object>>();
        for (int i=0;i<length;i++){
            HashMap<String,Object> map=new HashMap<String,Object>();
            map.put("ItemImage", imageRes[i]);//添加图像资源的ID
            map.put("ItemText", name[i]);//按序号做ItemText
            listImageRes.add(map);
            SimpleAdapter saImageItems = new SimpleAdapter(this,
                    listImageRes,//数据来源
                    R.layout.item,//item的XML实现

                    //动态数组与ImageItem对应的子项
                    new String[]{"ItemImage", "ItemText"},

                    //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                    new int[]{R.id.img_ruanjian, R.id.txt_ruanjian});
            //添加并且显示
            gridView.setAdapter(saImageItems);
            //添加消息处理
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(Main2Activity.this,name[position],Toast.LENGTH_LONG).show();
                }
            });

        }

    }
}
